const fileID = 'cloud://whiteny-yao-env-1ggz4djs9d880bf8.7768-whiteny-yao-env-1ggz4djs9d880bf8-1251927572/ChisatoTatsumi/10.jpg';
const fileSystemManager = wx.getFileSystemManager();
const TEMP_PIC_FILE_PATH_KEY = 'tempPicFilePathKey';
const USER_DATA_PATH = wx.env.USER_DATA_PATH;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadFromCloudBase : true,
    readFileBuffer : null,
    writeFileBufferPath : null,
    picUrl : null
  },

  async downloadPic() {
    //Load the pic from FileSystemManager
    let that = this;
    if(!this.data.loadFromCloudBase) {
      await fileSystemManager.readdir({
        dirPath : USER_DATA_PATH,
        success : function(res) {
          let files = res.files;
          console.log(`The 'res.files' =========== ${files}`);
          let picFile = files[1];
          console.log(`The 'res.files[1]' ========== ${picFile}`);
          
          let localPicFilePath = USER_DATA_PATH + "/" + picFile;
          that.setData({
             picUrl : localPicFilePath
          });

          console.log(`Download the test file locally into : ${localPicFilePath}`);
        }
      });
    }
    
    //Load the pic from Cloud Base
    else{
      let tempFilePath = null;
      await wx.cloud.downloadFile({
        fileID,
      })
      .then(res => {
        tempFilePath = res.tempFilePath;
        console.log(`Download the pic file by 'wx.cloud.downloadFile' into : ${res.tempFilePath}`);
      });
      
      fileSystemManager.saveFile({
        tempFilePath,
        filePath : USER_DATA_PATH + "/pic10.jpg",
        success : function(result) {
          let savedFilePath = result.savedFilePath;
          console.log(`Succeed to save the temp file into '${savedFilePath}'...`);

          that.setData({
            picUrl : savedFilePath
          });

          if(!wx.getStorageSync(TEMP_PIC_FILE_PATH_KEY)) {
            wx.setStorageSync(TEMP_PIC_FILE_PATH_KEY, savedFilePath);
            console.log(`Succeed to save the 'savedFilePath' into '${TEMP_PIC_FILE_PATH_KEY}' of Local Storage.`)
          }
        },
        fail : function(res) {
          let errMsg = res.errMsg;
          console.log(`Fail to save the file, due to : ${errMsg}`);
        }
      });
    }
  },

  switchFileLoadingMethod(event) {
    let loadFromCloudBase = event.target.dataset.loadfromcloudbase;

    this.setData({
      loadFromCloudBase : !loadFromCloudBase,
    });

    console.log(`loadFromCloudBase ========= ${this.data.loadFromCloudBase}`);
  
    return;
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})